const axios = require('axios').default;
const iconv = require('iconv-lite');
const cheerio = require('cheerio');

let header = {
  Connection: 'keep-alive',
  'User-Agent':
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
};

// 爬取页面
module.exports = (url, headers = header) => {
  return new Promise((resolve, reject) => {
    axios({
      url,
      headers
    })
      .then(res => {
        if (res && res.status === 200) {
          iconv.skipDecodeWarning = true;
          // const body = iconv.decode(res.data, 'GBK'); // 纯JS字符编码转换
          const body = res.data; // 此处不能进行转码处理
          resolve(cheerio.load(body));
        } else {
          reject(res.statusText);
        }
      })
      .catch(err => {
        console.log(err, '请求出错啦');
        reject(`页面请求失败,${err}`);
      });
  });
};
